<?php

// namespace System\Core;

if (!defined('BASEPATH')) die('Access Denied.');

/**
 * µMVC Framework (c) 2011.
 * 
 * PHP5 MVC Framework software for PHP 5.2.17 or later
 * 
 * @package         µMVC
 * @filename        system/core/errors.php
 * @copyright       Lanz (c) 2011.
 * 
 */

/**
 * Error handeling routines. Handles both regular errors and not catched
 * exceptions. This should probably be rewritten as a class later on.
 * 
 * @package         µMVC
 * @subpackage      Core
 * @category        System
 */

/*
*  Predefined error constants
*  http://www.php.net/manual/en/errorfunc.constants.php
* * 
1     E_ERROR
2     E_WARNING
4     E_PARSE
8     E_NOTICE
16    E_CORE_ERROR
32    E_CORE_WARNING
64    E_COMPILE_ERROR
128   E_COMPILE_WARNING
256   E_USER_ERROR
512   E_USER_WARNING
1024  E_USER_NOTICE
2048  E_STRICT
4096  E_RECOVERABLE_ERROR
8192  E_DEPRECATED
16384 E_USER_DEPRECATED
30719 E_ALL
*/         

function errorTemplate($errno, $errstr, $errfile, $errline, $type)
{   
    include(BASEPATH.'errors'.DS.'error.php');
}

/**
* Main Error Handler
* 
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
*/
function MyErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (($errno & error_reporting()) == 0 || ENVIRONMENT == 'production') return;  
    
    switch ($errno)
    {
        case E_ERROR:
            errorTemplate($errno, $errstr, $errfile, $errline, "ERROR");
        break;
        
        case E_WARNING:
            errorTemplate($errno, $errstr, $errfile, $errline, "WARNING");
        break;
        
        case E_PARSE:
            errorTemplate($errno, $errstr, $errfile, $errline, "PARSE ERROR");
        break;
        
        case E_NOTICE:
            errorTemplate($errno, $errstr, $errfile, $errline, "NOTICE");
        break;
                           
        case E_CORE_ERROR:
            errorTemplate($errno, $errstr, $errfile, $errline, "CORE ERROR");
        break;
        
        case E_CORE_WARNING:
            errorTemplate($errno, $errstr, $errfile, $errline, "CORE WARNING");
        break;
        
        case E_COMPILE_ERROR:
            errorTemplate($errno, $errstr, $errfile, $errline, "COMPILE ERROR");
        break;
        
        case E_USER_ERROR:
            errorTemplate($errno, $errstr, $errfile, $errline, "USER FATAL ERROR");
            exit();
        break;   
            
        case E_USER_WARNING:
            errorTemplate($errno, $errstr, $errfile, $errline, "USER WARNING");
        break;
        
        case E_USER_NOTICE:
            errorTemplate($errno, $errstr, $errfile, $errline, "USER NOTICE");
        break;
        
        case E_STRICT:
            errorTemplate($errno, $errstr, $errfile, $errline, "STRICT ERROR");
        break;
        
        case E_RECOVERABLE_ERROR:
            errorTemplate($errno, $errstr, $errfile, $errline, "RECOVERABLE ERROR");
        break;
        
        case E_DEPRECATED:
            errorTemplate($errno, $errstr, $errfile, $errline, "DEPRECATED ERROR");
        break;
        
        case E_USER_DEPRECATED:
            errorTemplate($errno, $errstr, $errfile, $errline, "USER DEPRECATED ERROR");
        break;
        
        case E_ALL:
            errorTemplate($errno, $errstr, $errfile, $errline, "ERROR");
        break;
    
    }

    /* Don't execute PHP internal error handler */
    return TRUE;
}          

/**
* Catch Exceptions
* 
* @param object $e
*/
function exceptionHandler($exception)
{      
    $title = 'Exception';
    include BASEPATH.'errors'.DS.'exception.php';
}       

function show_404($page)
{
    include BASEPATH.'errors'.DS.'404.php';
    exit;
}

set_error_handler('MyErrorHandler');
set_exception_handler('exceptionHandler');

?>
